package com.sanpu.kxe.business.member.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.member.bean.MemberPayTypeOutput;
import com.sanpu.kxe.common.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Description：会员充值方式列表
 * Created by 薛勇军 on 2019/3/19.
 */

public class MemberPayTypeAdapter extends BaseAdapter {

    private Context mContext;

    private List<MemberPayTypeOutput> payTypeOutputs = new ArrayList<>();

    public MemberPayTypeAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setNotifyData(List<MemberPayTypeOutput> outputs) {
        payTypeOutputs.clear();
        if (outputs != null) {
            payTypeOutputs.addAll(outputs);
        }
        this.notifyDataSetChanged();
    }

    /**
     * 选中item项
     */
    public void setNotifyData(int position) {
        if (payTypeOutputs != null && payTypeOutputs.size() > position) {
            for (MemberPayTypeOutput payTypeOutput: payTypeOutputs) {
                payTypeOutput.setChoose(false);
            }
            payTypeOutputs.get(position).setChoose(true);
        }
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (payTypeOutputs == null)
            return 0;
        return payTypeOutputs.size();
    }

    @Override
    public MemberPayTypeOutput getItem(int position) {
        if (payTypeOutputs == null)
            return null;
        return payTypeOutputs.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_member_pay, null);
        }
        ImageView payIv = ButterKnife.findById(convertView, R.id.payIv);
        TextView payTitleTv = ButterKnife.findById(convertView, R.id.payTitleTv);
        ImageView chooseIv = ButterKnife.findById(convertView, R.id.chooseIv);

        MemberPayTypeOutput payTypeOutput = getItem(position);
        if (payTypeOutput != null) {
            payIv.setImageResource(CommonUtils.getMemberPayDrawable(payTypeOutput.getShowIcon()));
            if (!TextUtils.isEmpty(payTypeOutput.getTitle())) {
                payTitleTv.setText(payTypeOutput.getTitle());
            }
            if (payTypeOutput.isChoose()) {
                chooseIv.setImageResource(R.drawable.ic_member_checked);
            } else {
                chooseIv.setImageResource(R.drawable.ic_choose_noraml);
            }
        }
        return convertView;
    }
}
